
#include <BPainter>
#include <BCursor>
#include <BFont>
#include <BMouseEvent>
#include <BKeyEvent>
#include <BScrollBar>

#include <BTreeWidget>
#include "member_BTreeWidget.h"
#include "member_BTreeItem.h"
#include "member_BWidget.h"

using namespace BWE;

#define member					(*(member_BTreeWidget*)_ptr)
#define member_allocate()		_ptr = new member_BTreeWidget(this)
#define member_release()		delete (member_BTreeWidget*)_ptr

BTreeWidget::BTreeWidget(BWidget* parent) : BScrollArea(parent)
{
	member_allocate();
}
BTreeWidget::~BTreeWidget()
{
	member_release();
}

void BTreeWidget::expandAll()
{
	if (member.topItems.size())
	{
		for (int i = 0; i < member.topItems.size(); i++)
		{
			BTreeItem* topItem = member.topItems[i];
			member.expandAll(topItem);
			member.freshWidget(topItem, true);
		}
		member.dirty = true;
		this->fresh();
	}
}
void BTreeWidget::shrinkAll()
{
	if (member.topItems.size())
	{
		for (int i = 0; i < member.topItems.size(); i++)
		{
			BTreeItem* topItem = member.topItems[i];
			member.shrinkAll(topItem);
			member.freshWidget(topItem, false);
		}
		member.dirty = true;
		this->fresh();
	}
}

void BTreeWidget::expand(BTreeItem* item)
{
	if (item && item_member(item)->tree == this && !item_member(item)->expanded)
	{
		item_member(item)->expanded = true;
		member.freshWidget(item, true);
		this->emit(Signal_ItemExpanded, item);
		member.dirty = true;
		this->fresh();
	}
}
void BTreeWidget::shrink(BTreeItem* item)
{
	if (item && item_member(item)->tree == this && item_member(item)->expanded)
	{
		item_member(item)->expanded = false;
		member.freshWidget(item, false);
		this->emit(Signal_ItemShrinked, item);
		member.dirty = true;
		this->fresh();
	}
}

void BTreeWidget::setIndent(int indent)
{
	if(member.indent != indent)
	{
		member.indent = indent;
		member.dirty = true;
		this->fresh();
	}
}
int BTreeWidget::indent() const
{
	return member.indent;
}

void BTreeWidget::setSelectMode(SelectMode selectMode)
{
	if (member.selectMode != selectMode)
	{
		member.selectMode = selectMode;
		this->clearSelection();
	}
}
SelectMode BTreeWidget::selectMode() const
{
	return member.selectMode;
}

void BTreeWidget::setSpace(int space)
{
	if (member.space != space)
	{
		member.space = space;
		this->fresh();
	}
}
int BTreeWidget::space() const
{
	return member.space;
}

void BTreeWidget::setArrowSize(const BSize& arrowSize)
{
	if (member.arrowSize != arrowSize)
	{
		member.arrowSize = arrowSize;
		this->fresh();
	}
}
const BSize& BTreeWidget::arrowSize() const
{
	return member.arrowSize;
}

void BTreeWidget::setIconSize(const BSize& iconSize)
{
	if (member.iconSize != iconSize)
	{
		member.iconSize = iconSize;
		this->fresh();
	}
}
const BSize& BTreeWidget::iconSize() const
{
	return member.iconSize;
}

bool BTreeWidget::addTopItem(BTreeItem* item)
{
	if (!item)
		return false;
	if (item_member(item)->tree == this)
		return true;
	member.topItems.append(item);
	if (item_member(item)->tree)
		item_member(item)->tree->removeItem(item);
	if (item_member(item)->parent)
		item_member(item)->parent->removeChild(item);
	item_member(item)->attach(this);
	member.dirty = true;
	this->fresh();
	return true;
}
BTreeItem* BTreeWidget::addTopItem(const BString& text)
{
	BTreeItemHolder item = new BTreeItem(text);
	if (addTopItem(item))
		return item;
	return 0;
}

bool BTreeWidget::removeTopItem(BTreeItem* item)
{
	if (!item)
		return false;
	if (item_member(item)->tree != this)
		return false;
	if (item_member(item)->parent)
		return false;
	item_member(item)->detach(this);
	member.selectedItems.remove(item);
	member.topItems.remove(item);
	member.dirty = true;
	this->fresh();
	return true;
}
bool BTreeWidget::removeTopItem(const BString& text)
{
	if (BTreeItem* item = topItem(text))
		return removeTopItem(item);
	return false;
}
bool BTreeWidget::removeItem(BTreeItem* item)
{
	if (!item)
		return false;
	if (item_member(item)->tree != this)
		return false;
	item_member(item)->detach(this);
	member.selectedItems.remove(item);
	if (item_member(item)->parent)
	{
		item->setParent(0);
		member.dirty = true;
		this->fresh();
		return true;
	}
	return removeTopItem(item);
}

void BTreeWidget::clearItems()
{
	if (member.topItems.size())
	{
		for (int i = 0; i < member.topItems.size(); i++)
		{
			BTreeItem* item = member.topItems[i];
			item_member(item)->detach(this);
		}
		member.topItems.clear();
		member.visualItems.clear();
		member.selectedItems.clear();
		this->fresh();
	}
}

int BTreeWidget::topItemCount() const
{
	return member.topItems.size();
}
BTreeItem* BTreeWidget::topItem(int index)
{
	return member.topItems(index);
}
const BTreeItem* BTreeWidget::topItem(int index) const
{
	return member.topItems(index);
}

BTreeItem* BTreeWidget::topItem(const BString& text)
{
	for (int i = 0; i < member.topItems.size(); i++)
	{
		BTreeItem* topItem = member.topItems[i];
		if (topItem->text() == text)
			return topItem;
	}
	return 0;
}
const BTreeItem* BTreeWidget::topItem(const BString& text) const
{
	for (int i = 0; i < member.topItems.size(); i++)
	{
		const BTreeItem* topItem = member.topItems[i];
		if (topItem->text() == text)
			return topItem;
	}
	return 0;
}

BTreeItem* BTreeWidget::itemAt(const BPoint& point)
{
	BRect crect = this->centerRect();
	if(!crect.contain(point))
		return 0;
	const BRect& area = this->area();
	for (int i = 0; i < member.visualItems.size(); i++)
	{
		BTreeItem* item = member.visualItems[i];
		if (!item_member(item)->visible)
			continue;
		BRect rect = item_member(item)->rect + area.pos();
		rect.left() = crect.left();
		rect.right() = crect.right();
		if (rect.contain(point))
			return item;
	}
	return 0;
}
const BTreeItem* BTreeWidget::itemAt(const BPoint& point) const
{
	BRect crect = this->centerRect();
	if (!crect.contain(point))
		return 0;
	const BRect& area = this->area();
	for (int i = 0; i < member.visualItems.size(); i++)
	{
		BTreeItem* item = member.visualItems[i];
		if (!item_member(item)->visible)
			continue;
		BRect rect = item_member(item)->rect + area.pos();
		rect.left() = crect.left();
		rect.right() = crect.right();
		if (rect.contain(point))
			return item;
	}
	return 0;
}

BTreeItem* BTreeWidget::itemOfText(const BString& text)
{
	for (int i = 0; i < member.topItems.size(); i++)
	{
		BTreeItem* topItem = member.topItems[i];
		BTreeItem* item = topItem->findByText(text);
		if (item)
			return item;
	}
	return 0;
}
const BTreeItem* BTreeWidget::itemOfText(const BString& text) const
{
	for (int i = 0; i < member.topItems.size(); i++)
	{
		BTreeItem* topItem = member.topItems[i];
		BTreeItem* item = topItem->findByText(text);
		if (item)
			return item;
	}
	return 0;
}

BTreeItem* BTreeWidget::itemOfAsset(const BValue& value)
{
	for (int i = 0; i < member.topItems.size(); i++)
	{
		BTreeItem* topItem = member.topItems[i];
		BTreeItem* item = topItem->findByAsset(value);
		if (item)
			return item;
	}
	return 0;
}
const BTreeItem* BTreeWidget::itemOfAsset(const BValue& value) const
{
	for (int i = 0; i < member.topItems.size(); i++)
	{
		BTreeItem* topItem = member.topItems[i];
		BTreeItem* item = topItem->findByAsset(value);
		if (item)
			return item;
	}
	return 0;
}

BTreeItem* BTreeWidget::create(const BString& path)
{
	int end = path.find('/');
	if (end == path.size())
	{
		BTreeItem* item = this->topItem(path);
		if (item == 0)
			item = this->addTopItem(path);
		return item;
	}
	BString text;
	path.substr(text, 0, end);
	BTreeItem* item = this->topItem(text);
	if (item == 0)
	{
		item = this->addTopItem(text);
	}
	int pos = end + 1;
	while (item && pos < path.size())
	{
		end = path.find('/', pos);
		path.substr(text, pos, end);
		BTreeItem* child = item->child(text);
		if (child == 0)
			child = item->addChild(text);
		item = child;
		pos = end + 1;
	}
	return item;
}

BTreeItem* BTreeWidget::locate(const BString& path)
{
	int end = path.find('/');
	if (end == path.size())
		return this->topItem(path);
	BString text;
	path.substr(text, 0, end);
	BTreeItem* item = this->topItem(text);
	int pos = end + 1;
	while(item && pos < path.size())
	{
		end = path.find('/', pos);
		path.substr(text, pos, end);
		item = item->child(text);
		pos = end + 1;
	}
	return item;
}
const BTreeItem* BTreeWidget::locate(const BString& path) const
{
	int end = path.find('/');
	if (end == path.size())
		return this->topItem(path);
	BString text;
	path.substr(text, 0, end);
	const BTreeItem* item = this->topItem(text);
	int pos = end + 1;
	while (item && pos < path.size())
	{
		end = path.find('/', pos);
		path.substr(text, pos, end);
		item = item->child(text);
		pos = end + 1;
	}
	return item;
}

bool BTreeWidget::contain(const BTreeItem* item)
{
	if (!item)
		return false;
	for (int i = 0; i < member.topItems.size(); i++)
	{
		BTreeItem* topItem = member.topItems[i];
		if (topItem->contain(item))
			return true;
	}
	return false;
}

bool BTreeWidget::setSelection(const BTreeItemList& selection)
{
	bool selectionChanged = false;
	for (auto it = member.selectedItems.begin(); it.valid(); ++it)
	{
		BTreeItem* item = *it;
		if (!selection.contain(item))
		{
			item_member(item)->selected = false;
			member.selectedItems.erase(it);
			emit(Signal_ItemSelected, item);
			selectionChanged = true;
		}
	}
	for (auto it = selection.begin(); it.valid(); ++it)
	{
		BTreeItem* item = *it;
		if (!member.selectedItems.contain(item))
		{
			item_member(item)->selected = true;
			member.selectedItems.append(item);
			emit(Signal_ItemSelected, item);
			selectionChanged = true;
		}
	}
	member.selectedItems = selection;
	if (selectionChanged)
	{
		emit(Signal_SelectionChanged);
		this->fresh();
		return true;
	}
	return false;
}
const BTreeItemList& BTreeWidget::selection() const
{
	return member.selectedItems;
}

bool BTreeWidget::setItemSelected(BTreeItem* item, bool selected)
{
	if (!item)
	{
		return false;
	}
	if (selected)
	{
		if (member.selectMode & Select_Multiple)
		{
			if (!member.selectedItems.contain(item))
			{
				member.selectedItems.append(item);
				item_member(item)->selected = true;
				emit(Signal_ItemSelected, item);
				emit(Signal_SelectionChanged);
				this->fresh();
				return true;
			}
		}
		else
		{
			for (int i = 0; i < member.selectedItems.size(); i++)
			{
				BTreeItem* selectedItem = member.selectedItems[i];
				if (selectedItem != item)
				{
					member.selectedItems.remove(i--);
					item_member(selectedItem)->selected = false;
					emit(Signal_ItemSelected, selectedItem);
				}
			}
			if (!member.selectedItems.contain(item))
			{
				member.selectedItems.append(item);
				item_member(item)->selected = true;
				emit(Signal_ItemSelected, item);
			}
			emit(Signal_SelectionChanged);
			this->fresh();
		}
	}
	else
	{
		if (member.selectMode & Select_Multiple)
		{
			if (member.selectedItems.contain(item))
			{
				member.selectedItems.remove(item);
				item_member(item)->selected = false;
				emit(Signal_ItemSelected, item);
				emit(Signal_SelectionChanged);
				this->fresh();
				return true;
			}
		}
		else
		{
			for (int i = 0; i < member.selectedItems.size(); i++)
			{
				BTreeItem* selectedItem = member.selectedItems[i];
				if (selectedItem != item)
				{
					member.selectedItems.remove(i--);
					item_member(selectedItem)->selected = false;
					emit(Signal_ItemSelected, selectedItem);
				}
			}
			if (member.selectedItems.remove(item))
			{
				item_member(item)->selected = false;
				emit(Signal_ItemSelected, item);
			}
			emit(Signal_SelectionChanged);
			this->fresh();
		}
	}
	return true;
}
bool BTreeWidget::clearSelection()
{
	if (member.selectedItems.empty())
		return false;
	while (BTreeItem* item = member.selectedItems.last())
	{
		member.selectedItems -= item;
		item_member(item)->selected = false;
		emit(Signal_ItemSelected, item);
	}
	emit(Signal_SelectionChanged);
	this->fresh();
	return true;
}

int BTreeWidget::selectedItemCount() const
{
	return member.selectedItems.size();
}
BTreeItem* BTreeWidget::selectedItem(int index)
{
	return member.selectedItems(index);
}
const BTreeItem* BTreeWidget::selectedItem(int index) const
{
	return member.selectedItems(index);
}

bool BTreeWidget::itemVisual(BTreeItem* item) const
{
	return member.visualItems.contain(item);
}
bool BTreeWidget::scrollToItem(BTreeItem* item, Align align)
{
	if (!item || item_member(item)->tree != this)
		return false;
	if (member.dirty)
	{
		member.dirty = false;
		member.freshArea();
	}
	BRect crect = this->centerRect();
	BRect area = this->area();
	if (item)
	{
		BRect rect0 = item_member(item)->rect + area.pos();
		BRect rect1 = crect.align(align, rect0.size());
		BPoint offset = rect1.pos() - rect0.pos();
		area.move(offset);
	}
	else
	{
		area = crect.align(align, area.size());
	}
	this->setArea(area);
	return true;
}

bool BTreeWidget::query(const BString& text, BValue& value) const
{
	if (text == "visual-items")
	{
		BTreeItemArray& items = value.fill<BTreeItemArray>();
		items = member.visualItems;
		return true;
	}
	return BScrollArea::query(text, value);
}

void BTreeWidget::scrollArea()
{
	member.visualItems.reset();
	const BRect& area = this->area();
	const BRect& crect = this->centerRect();
	for (int i = 0; i < member.topItems.size(); i++)
	{
		BTreeItem* item = member.topItems[i];
		if (!item_member(item)->visible)
			continue;
		member.adjustVisualItem(item, area.pos(), crect);
	}
	this->fresh();
	BScrollArea::scrollArea();
}

void BTreeWidget::resizeEvent(const BEvent& event)
{
	BScrollArea::resizeEvent(event);
	member.dirty = true;
	this->fresh();
}

void BTreeWidget::styleEvent(const BEvent& event)
{
	BScrollArea::styleEvent(event);
	if (const BStyle* style = event.value())
	{
		const BValue& iconSizeValue = style->value(Value_Icon_Size);
		if (iconSizeValue.valid())
			member.iconSize = iconSizeValue;

		const BValue& arrowSizeValue = style->value(Value_Arrow_Size);
		if (arrowSizeValue.valid())
			member.arrowSize = arrowSizeValue;

		BIcon* expandIcon = style->icon(Icon_Expand);
		if (expandIcon)
			member.expandIcon = expandIcon;

		BIcon* shrinkIcon = style->icon(Icon_Shrink);
		if (shrinkIcon)
			member.shrinkIcon = shrinkIcon;

		if (const BStyle* itemStyle = style->sub("item"))
		{
			const BValue& alignValue = itemStyle->value(Value_Align);
			if (alignValue.valid())
				member.itemAlign = alignValue;

			const BValue& formValue = itemStyle->value(Value_Form);
			if (formValue.valid())
				member.itemForm = formValue;

			const BValue& itemWidthValue = itemStyle->value(Value_Width);
			if (itemWidthValue.valid())
				member.itemSize.width() = itemWidthValue;

			const BValue& itemHeightValue = itemStyle->value(Value_Height);
			if (itemHeightValue.valid())
				member.itemSize.height() = itemHeightValue;

		}
	}
	for (int i = 0; i < member.topItems.size(); i++)
	{
		BTreeItem* topItem = member.topItems[i];
		member.dirtyItem(topItem);
	}
	member.dirty = true;
}
void BTreeWidget::freshEvent(const BEvent& event)
{
	BScrollArea::freshEvent(event);
	if (member.dirty)
	{
		member.dirty = false;
		member.freshArea();
		scrollArea();
	}
}
void BTreeWidget::paintEvent(const BEvent& event)
{
	BSize size = this->size();
	const BRect& crect = this->centerRect();
	const BRect& area = this->area();
	const BPoint& offset = this->offset();

	BPainter painter(this);
	painter.drawBackground();
	painter.drawPerch();

	const BFont* font = painter.font();

	painter.setClip(crect);
	for (int i = 0; i < member.visualItems.size(); i++)
	{
		const BTreeItem* item = member.visualItems[i];
		StyleRole colorRole = Color_Item;
		State iconState = State_Normal;
		if(item->isSelected())
		{
			colorRole = Color_Item_Checked;
			iconState = State_Selected;
		}
		else if(member.hoveredItem == item)
		{
			colorRole = Color_Item_Hovered;
			iconState = State_Hovered;
		}
		if (painter.setColor(colorRole))
		{
			BRect rect = item_member(item)->rect + offset;
			rect.right() = crect.right();
			painter.fillRect(rect);
		}
		if (item->childCount())
		{
			StyleRole colorArrowRole = Color_Arrow;
			if (member.hoveredItem == item)
			{
				if (member.pressed)
					colorArrowRole = Color_Arrow_Pressed;
				else
					colorArrowRole = Color_Arrow_Hovered;
			}
			State arrowState = State_Normal;
			if (member.hoveredArrow)
			{
				if (member.pressed)
					arrowState = State_Pressed;
				else
					arrowState = State_Hovered;
			}
			painter.setColor(colorArrowRole);
			if (item->expanded())
			{
				if (member.expandIcon)
					painter.drawImage(item_member(item)->arrowRect, member.expandIcon->image(arrowState));
				else
					painter.drawGraph(item_member(item)->arrowRect, Graph_Arrow_Down_Fill);
			}
			else
			{
				if (member.shrinkIcon)
					painter.drawImage(item_member(item)->arrowRect, member.shrinkIcon->image(arrowState));
				else
					painter.drawGraph(item_member(item)->arrowRect, Graph_Arrow_Right_Fill);
			}
		}
		if (item_member(item)->ia_imageRect.valid())
		{
			painter.setColor(item_member(item)->color);
			BImage* image = 0;
			if (item_member(item)->icon)
			{
				if (item_member(item)->selected)
					image = item_member(item)->icon->image(State_Selected);
				else if (member.hoveredItem == item)
					image = item_member(item)->icon->image(State_Hovered);
				else
					image = item_member(item)->icon->image(State_Normal);
			}
			else if (item_member(item)->image)
			{
				image = item_member(item)->image;
			}
			if (image)
			{
				painter.drawImage(item_member(item)->ia_imageRect, image, true);
			}
			else
			{
				if (item_member(item)->graph && item_member(item)->color.a() > 1)
				{
					painter.setColor(item_member(item)->color);
					painter.drawGraph(item_member(item)->ia_imageRect, item_member(item)->graph);
				}
			}
		}
		const BString& text = item_member(item)->text;
		if (text.size())
		{
			painter.setFont(item_member(item)->realFont);
			painter.setColor(Color_Text);
			painter.drawText(item_member(item)->ia_textRect, item_member(item)->text);
		}
	}
	painter.setClip(0, 0, this->width(), this->height());

	painter.drawMargin();
	painter.drawBorder();
}

void BTreeWidget::mouseEnterEvent(const BMouseEvent& mouseEvent)
{
	if (member.hoveredItem)
	{
		member.hoveredItem = 0;
		this->fresh();
	}
	BScrollArea::mouseLeaveEvent(mouseEvent);
}
void BTreeWidget::mouseLeaveEvent(const BMouseEvent& mouseEvent)
{
	if (member.hoveredItem)
	{
		member.hoveredItem = 0;
		this->fresh();
	}
	BScrollArea::mouseLeaveEvent(mouseEvent);
}

void BTreeWidget::mousePressEvent(const BMouseEvent& mouseEvent)
{
	if (mouseEvent.button() == Button_Left)
	{
		member.pressed = true;
		this->fresh();
	}
	BScrollArea::mousePressEvent(mouseEvent);
}
void BTreeWidget::mouseReleaseEvent(const BMouseEvent& mouseEvent)
{
	if (mouseEvent.button() == Button_Left)
	{
		member.pressed = false;
		this->fresh();
	}
	BScrollArea::mouseReleaseEvent(mouseEvent);
}
void BTreeWidget::mouseMoveEvent(const BMouseEvent& mouseEvent)
{
	const BPoint& pos = mouseEvent.pos();
	BTreeItem* hoveredItem = this->itemAt(pos);
	if(member.hoveredItem != hoveredItem)
	{
		member.hoveredItem = hoveredItem;
		emit(Signal_ItemHovered, hoveredItem);
		this->fresh();
	}
	if(hoveredItem)
	{
		bool hoveredArrow = item_member(hoveredItem)->arrowRect.contain(pos);
		if(member.hoveredArrow != hoveredArrow)
		{
			member.hoveredArrow = hoveredArrow;
			this->fresh();
		}
	}
	else
	{
		if(member.hoveredArrow)
		{
			member.hoveredArrow = false;
			this->fresh();
		}
	}
	BScrollArea::mouseMoveEvent(mouseEvent);
}

void BTreeWidget::mouseClickEvent(const BMouseEvent& mouseEvent)
{
	if (mouseEvent.button() == Button_Left)
	{
		if (member.hoveredArrow)
		{
			if (member.hoveredItem->expanded())
				this->shrink(member.hoveredItem);
			else
				this->expand(member.hoveredItem);
			if (member.hoveredItem->childCount() > 0)
			{
				this->fresh();
			}
		}
		else
		{
			if (member.selectMode & Select_Single)
			{
				BTreeItem* item = member.hoveredItem;
				if (item)
				{
					bool selectionChanged = false;
					for (auto it = member.selectedItems.begin(); it.valid(); ++it)
					{
						if (*it != item)
						{
							BTreeItem* temp = *it;
							member.selectedItems.erase(it);
							item_member(temp)->selected = false;
							emit(Signal_ItemSelected, temp);
							it--;
							selectionChanged = true;
						}
					}
					if (member.selectedItems.empty())
					{
						member.selectedItems.append(item);
						item_member(item)->selected = true;
						emit(Signal_ItemSelected, item);
						selectionChanged = true;
					}
					if (selectionChanged)
					{
						emit(Signal_SelectionChanged);
						this->fresh();
					}
				}
				else
				{
					clearSelection();
				}
			}
			if (member.selectMode & Select_Multiple)
			{
				BTreeItem* item = member.hoveredItem;
				if (item == 0)
				{
					clearSelection();
				}
				else if (mouseEvent.keyState(Key_LCtrl))
				{
					bool selected = !item_member(item)->selected;
					setItemSelected(item, selected);
				}
				else if (member.selectMode & Select_Contiguous && mouseEvent.keyState(Key_LShift))
				{
					if (member.selectedItems.empty())
					{
						setItemSelected(item, true);
					}
					else if (member.selectedItems.first() != item)
					{
						bool selectionChanged = false;
						BTreeItemList selection;
						BTreeItem* firstItem = member.selectedItems.first();
						if (firstItem->rect().top() < item->rect().top())
						{
							member.collectNextItems(firstItem, item, selection);
						}
						else
						{
							member.collectPrevItems(firstItem, item, selection);
						}
						this->setSelection(selection);
					}
				}
				else
				{
					bool selectionChanged = false;
					for (auto it = member.selectedItems.begin(); it.valid(); ++it)
					{
						if (*it != item)
						{
							BTreeItem* temp = *it;
							member.selectedItems.erase(it);
							item_member(temp)->selected = false;
							emit(Signal_ItemSelected, temp);
							it--;
							selectionChanged = true;
						}
					}
					if (item && member.selectedItems.empty())
					{
						member.selectedItems.append(item);
						item_member(item)->selected = true;
						emit(Signal_ItemSelected, item);
						selectionChanged = true;
					}
					if (selectionChanged)
					{
						emit(Signal_SelectionChanged);
						this->fresh();
					}
				}
			}
		}
	}
	if (!member.hoveredArrow)
	{
		if (mouseEvent.button() == Button_Left)
		{
			emit(Signal_ItemClicked, member.hoveredItem);
		}
		if (mouseEvent.button() == Button_Right)
		{
			emit(Signal_ItemRightClicked, member.hoveredItem);
		}
	}
	BScrollArea::mouseClickEvent(mouseEvent);
}
void BTreeWidget::mouseDoubleClickEvent(const BMouseEvent& mouseEvent)
{
	if (!member.hoveredArrow && member.hoveredItem)
	{
		if (mouseEvent.button() == Button_Left)
		{
			BTreeItem* item = member.hoveredItem;
			emit(Signal_ItemDoubleClicked, item);
			this->fresh();
		}
	}
	BScrollArea::mouseDoubleClickEvent(mouseEvent);
}
